import { Plugin } from '@nuxt/types'

import { Resources } from '@/types/resources'
import { ConnectorQuery, ConnectorPost, ConnectorUpload, ConnectorDownload } from '@/types/connector'

import connector from '@/resources/connector'
import resources from '~/resources'

declare module 'vue/types/vue' {
  interface Vue {
    $resources: Resources;
    $post: ConnectorPost;
    $query: ConnectorQuery;
    $upload: ConnectorUpload;
    $download: ConnectorDownload;
  }
}

declare module '@nuxt/types' {
  interface NuxtAppOptions {
    $resources: Resources;
    $post: ConnectorPost;
    $query: ConnectorQuery;
    $upload: ConnectorUpload;
    $download: ConnectorDownload;
  }
}

declare module 'vue/types/index' {
  interface Store<S> {
    $resources: Resources;
    $post: ConnectorPost;
    $query: ConnectorQuery;
    $upload: ConnectorUpload;
    $download: ConnectorDownload;
  }
}

const resourcesPlugin: Plugin = (ctx, inject) => {
  const connectRes = connector(ctx)
  Object.keys(connectRes).forEach((key: string) => {
    // @ts-ignore
    inject(key, connectRes[key])
  })
  inject('resources', resources(connectRes))
}

export default resourcesPlugin
